// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    bgColor: cc.Color = new cc.Color().fromHEX('#ffffff')

    speedXnum: number = 5; // x偏移速度

    directionX: number = 1; // 偏移方向 正数正向 负数反向

    onLoad () {
        this.node.scale = 0.2 + Math.random() * 0.5;
        this.speedXnum = 1 + Math.random() * 5;

        if(Math.random() > 0.5){
            this.directionX = -1;
        }
        
        const maxH = 50 + Math.random() * 200;
        const minH = -maxH - Math.random() * 20;
        // const delyTimer: number = Math.random() * 0.5;
        const aniTimer: number = 0.2 + Math.random() * 0.2;
        cc.tween(this.node)
            // .delay(delyTimer)
            .by(aniTimer, {y: maxH}, {easing: 'quadOut'})
            .by(aniTimer, {y: minH}, {easing: 'quadIn'})
            .call(() => {
                this.node.removeFromParent();
            })
            .start()
        
    }

    // start () {}

    update (dt) {
        this.node.x += this.speedXnum * this.directionX
    }

    changeColorHandler(pColor){
        this.node.color = pColor;
        this.bgColor = pColor;
    }
}
